<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="站内信标题">
                <a-input v-model="queryParam.title" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="创建时间">
                <a-range-picker :allowClear="true" ref="createdAtRangePicker" @change="createdAtRangePickerOnChange"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="() => this.$router.push('/proxy/website-letter/create')">新建
        </a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :scroll="{ x: 1300 }"
        :data="loadData"
        showPagination="auto"
      >
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">编辑</a>

            <a-divider type="vertical"/>

            <a-popconfirm placement="top" @confirm="confirmDelete(record)">
              <template slot="title">
                确定要删除吗?
              </template>
              <a>删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </a-card>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import { deleteWebsiteLetter, getWebsiteLetterList } from '@/api/proxy'

  export default {
    name: 'List',
    components: {
      STable
    },
    data () {
      return {
        formVisible: false,
        formRaw: {},
        mdl: {},
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '序号',
            fixed: 'left',
            dataIndex: 'id'
          },
          {
            title: '标题',
            dataIndex: 'title'
          },
          {
            title: '内容',
            dataIndex: 'content'
          },
          {
            title: '跳转链接',
            dataIndex: 'link'
          },
          {
            title: '已读人数',
            dataIndex: 'number_read'
          },
          {
            title: '创建时间',
            dataIndex: 'created_at'
          },
          {
            title: '操作',
            dataIndex: 'action',
            fixed: 'right',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return getWebsiteLetterList(Object.assign(parameter, this.queryParam)).then(res => {
            return res
          })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created () {

    },
    methods: {
      handleEdit (record) {
        this.$router.push(`/proxy/website-letter/edit/${record.id}`)
      },
      confirmDelete (record) {
        deleteWebsiteLetter(record.id).then(() => this.$refs.table.refresh())
      },
      formVisibleEvent (val) {
        this.formVisible = val
        this.formRaw = {}
      },
      refreshList () {
        this.$refs.table.refresh()
      },
      createdAtRangePickerOnChange (date, dateString) {
        this.queryParam.created_at_begin = dateString[0]
        this.queryParam.created_at_end = dateString[1]
      },
      handleReset () {
        this.queryParam = {}
        this.$refs.createdAtRangePicker.$refs.picker.clearSelection(new MouseEvent('click'))
      }
    }
  }
</script>
